package com.xiaomi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaomi.domain.CategoryEntity;
import com.xiaomi.mappers.CategoryMapper;
import com.xiaomi.service.CateService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class CateServiceImpl implements CateService {
    @Autowired
   private  CategoryMapper categoryMapper;

    @Override
    public List<CategoryEntity> findAllCate() {
        List<CategoryEntity> categoryEntityList = new ArrayList<CategoryEntity>();
        try{
            categoryEntityList =categoryMapper.findAll();


        }catch (Exception e){
            e.printStackTrace();
        }

        return categoryEntityList;
    }

    @Override
    public PageInfo<CategoryEntity> findAllCates(int pageNum,int pageSize) throws IOException {
        //设置分页信息
        if(pageSize==0){
            pageSize=5;
        }
        PageHelper.startPage(pageNum, pageSize);
        //查询所有记录
        List<CategoryEntity> categoryEntityList = new ArrayList<CategoryEntity>();
        Set s=null;
        try{
          categoryEntityList =categoryMapper.findAll();


        }catch (Exception e){
            e.printStackTrace();
        }



        return new PageInfo<>(categoryEntityList);
    }

    @Override
    public void cateAlter(CategoryEntity categoryEntity) {
        System.out.println(categoryEntity);
        categoryMapper.cateUpdate(categoryEntity);
        return;
    }

    @Override
    public void addCate(CategoryEntity categoryEntity) {
     categoryMapper.cateIn(categoryEntity);
     return;
    }

    @Override
    public CategoryEntity findById(String cid) {
        CategoryEntity categoryEntity=categoryMapper.findById(cid);
        return  categoryEntity;
    }

    @Override
    public void dele(int id) {

    }

    @Override
    public void delets(String[] ids) {
        categoryMapper.cateDels(ids);
        return;
    }
}
